package com.ecarx.module_udisk;


import android.view.View;
import android.widget.Toast;

import com.ecarx.library_base.base_fragment.BaseFragment;
import com.ecarx.library_base.usb.UDiskDevice;
import com.ecarx.library_base.usb.UDiskUtil;
import com.ecarx.module_udisk.recoveryos.IRecoveryCallback;
import com.ecarx.module_udisk.recoveryos.RecoveryTrigger;

import java.util.List;


public class UDiskOtaFragment extends BaseFragment implements View.OnClickListener, IRecoveryCallback {


    public UDiskOtaFragment() {
        // Required empty public constructor

    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_u_disk_ota;
    }

    @Override
    protected void init(View view) {
        view.findViewById(R.id.btn_lunch_recoveryos).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_lunch_recoveryos) {
            if (UDiskUtil.getUDiskDeviceList().size() <= 0) {
                showToast(getString(R.string.lunch_no_udisk));
                return;
            }
            RecoveryTrigger.getInstance().setCallback(this).lunch();
        }
    }

    @Override
    public void onRecoveryGetMessage() {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast(getString(R.string.lunch_recoveryos_msg_get));
                }
            });
        }
    }

    @Override
    public void onTrigger(boolean success) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (success) {
                        showToast(getString(R.string.lunch_recoveryos_success));
                    } else {
                        showToast(getString(R.string.lunch_recoveryos_fail));
                    }
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RecoveryTrigger.getInstance().release();
    }
}